package puzzle.projecteuler.p100;

import java.util.ArrayList;
import java.util.List;

public class Problem074 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		int c = 0;
		for (long i = 1; i < 1000000; i ++) {
			if (circle(i).size() == 60) {
				c ++;
			}
		}
		System.out.println(c);
	}
	
	private static int[] frac = new int[] {
		1,1,2,6,24,120,720,5040,40320,362880
	};

	public static long next(long n) {
		long s = 0;
		while (n>0) {
			s += frac[(int)(n%10)];
			n /= 10;
		}
		return s;
	}
	
	public static List<Long> circle(long n) {
		
		List<Long> list = new ArrayList<Long>();
		list.add(n);
		long c = next(n);
		while (!list.contains(c)) {
			list.add(c);
			c = next(c); 
		}
		return list;
	}
}
